package it.com.cci.observer_pattern;


import java.util.ArrayList;
import java.util.List;

/**
 * @author Lefnmg
 * @Description: 天气主题
 * @date 2018/3/25
 */
public class WeatherDataSubject implements Subject {
    private float temp; //温度
    private float pressure; //压力
    private float humidity; //湿度
    List<Observer> list = new ArrayList<>(); //观察者对象集合
    @Override
    public void registerObserver(Observer observer) {
        list.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        int i = list.indexOf(observer);
        if (i>=0){
            list.remove(observer);
        }
    }

    @Override
    public void notifyObservers() {
        for (Observer observer :list){
            observer.update(temp,humidity,pressure);
        }
    }
    //天气数据变化是通知所有观察者
    public void measurementsChanged(){
        notifyObservers();
    }
    //获取变化数据
    public void setMeasurements(float temp,float humidity,float pressure){
        this.temp = temp;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }
    //
}
